package com.qfedu.microuser.dailyReport.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qfedu.microuser.dailyReport.entity.DailyReport;
import com.qfedu.microuser.dailyReport.mapper.DailyReportMapper;
import com.qfedu.microuser.dailyReport.server.DailyReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DailyReportServiceImpl extends ServiceImpl<DailyReportMapper, DailyReport> implements DailyReportService {

    @Resource
    private  DailyReportMapper dailyReportMapper;

    @Override
    public boolean submitDaily(DailyReport dailyReport) {
        dailyReport.setCreateTime(LocalDateTime.now()); // 设置创建时间
        return save(dailyReport); // MyBatis-Plus 自带的保存方法
    }




    @Override
    public List<DailyReport> getLogsByDate(LocalDate date) {
        if (date == null) {
            return getAllLogs(); // 日期为 null 时，查询所有日志
        }
        // 构造“操作时间在指定日期当天”的查询条件
        QueryWrapper<DailyReport> wrapper = new QueryWrapper<>();
        wrapper.between(
                "operate_time",
                date.atStartOfDay(),            // 当天 00:00:00
                date.plusDays(1).atStartOfDay().minusSeconds(1) // 次日 00:00:00 前1秒
        );
        return dailyReportMapper.selectList(wrapper);
    }

    @Override
    public List<DailyReport> getAllLogs() {
        return dailyReportMapper.selectList(null); // 查询所有日志
    }
}
